using System;
using System.Collections;
using System.Collections.Generic;
using BCWAccountVip;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using I2.Loc;
using UnityEngine;

public class PanelEndGameRewardUI : MonoBehaviour
{
	private const string KEY_SAVE_REWARD_SIMPLE = "keySaveRewardSimple";

	private const string SESSION_REPUTATION_CANGED = "keySessionReputationChanged";

	private const string SESSION_DEAD_COUNT = "keySesionDeadCount";

	private const string SESSION_KILL_COUNT = "keySesionKillCount";

	private const string SESSION_LEADER_TIME = "keySessionleaderTime";

	private const string SESSION_CUPS = "keySessionCups";

	public static PanelEndGameRewardUI instance;

	private EventDelegate.Callback _callback;

	[SerializeField]
	private GameObject panelEndGameExample;

	[SerializeField]
	private UIGrid grid;

	[SerializeField]
	private GameObject panelReward;

	[SerializeField]
	private UILabel killCount;

	[SerializeField]
	private UILabel deadCount;

	[SerializeField]
	private UILabel repCount;

	[SerializeField]
	private UILabel leaderTime;

	[SerializeField]
	private GameObject leaderTimeObject;

	[SerializeField]
	private PanelEndGameButton button;

	private List<SessionReputationReward> _rewardListSimple = new List<SessionReputationReward>();

	private List<SessionReputationReward> _rewardListVip = new List<SessionReputationReward>();

	private List<SessionReputationReward> _rewardForSave = new List<SessionReputationReward>();

	private bool _isActiveVip;

	private bool _blockGiveAward;

	private bool _isCalculateReward;

	private bool _hasSaveReward;

	private int _sessionReputationChanged;

	private int _sessionDeadCount;

	private int _sessionKillCount;

	private float _leaderTime;

	private int _sessionCups;

	private bool _isShow;

	private GameObject panelAccountVipBuy;

	private GameObject _noVipReward;

	private GameObject _vipReward;

	public static bool isGiveReward;

	[HideInInspector]
	public int counter;

	private bool animationInProgress;

	public EventDelegate.Callback Callback
	{
		set
		{
			_callback = value;
		}
	}

	public PanelEndGameButton Button
	{
		get
		{
			return button;
		}
	}

	public List<SessionReputationReward> RewardListSimple
	{
		get
		{
			return _rewardListSimple;
		}
	}

	public bool IsActiveVip
	{
		set
		{
			_isActiveVip = value;
		}
	}

	public int SessionKillCount
	{
		get
		{
			return _sessionKillCount;
		}
	}

	public bool IsShow
	{
		get
		{
			return _isShow;
		}
	}

	public static event Action giveReward;

	private void Awake()
	{
		instance = this;
		isGiveReward = false;
		panelAccountVipBuy = UnityEngine.Object.Instantiate(Resources.Load<GameObject>("Prefabs/AccountVipWindow"));
		panelAccountVipBuy.transform.SetParent(base.transform.parent);
		panelAccountVipBuy.transform.localScale = Vector3.one;
		panelAccountVipBuy.transform.localPosition = Vector3.zero;
		panelAccountVipBuy.gameObject.SetActive(false);
	}

	private void OnDesctroy()
	{
		instance = null;
	}

	private void OnEnable()
	{
		InAppShop.eventBuyAccount += BuyAccountVipSuccsesful;
	}

	private void OnDisable()
	{
		InAppShop.eventBuyAccount -= BuyAccountVipSuccsesful;
	}

	public void BuyAccountVipSuccsesful()
	{
		_isActiveVip = true;
		PanelEndGameReward component = _noVipReward.GetComponent<PanelEndGameReward>();
		component.ActiveFrame.SetActive(false);
		PanelEndGameReward component2 = _vipReward.GetComponent<PanelEndGameReward>();
		component2.ActiveFrame.SetActive(true);
		component2.ButtonBuyVip.SetActive(false);
		component2.LabelBuyVip.SetActive(true);
	}

	public void Show()
	{
		CalculateReward();
		StartCoroutine(SetCountLabel(repCount, _sessionReputationChanged));
		StartCoroutine(SetCountLabel(deadCount, _sessionDeadCount));
		StartCoroutine(SetCountLabel(killCount, _sessionKillCount));
		if (_leaderTime > 0f)
		{
			leaderTimeObject.SetActive(true);
			StartCoroutine(SetCountLabel(leaderTime, (int)_leaderTime));
		}
		else
		{
			leaderTimeObject.SetActive(false);
		}
		if (_rewardListSimple.Count > 0)
		{
			ShowReward();
		}
		else
		{
			panelReward.SetActive(false);
		}
	}

	private void ShowReward()
	{
		_isShow = true;
		_noVipReward = UnityEngine.Object.Instantiate(panelEndGameExample);
		_noVipReward.transform.SetParent(grid.transform);
		_noVipReward.transform.localScale = Vector3.one;
		_noVipReward.SetActive(true);
		PanelEndGameReward component = _noVipReward.GetComponent<PanelEndGameReward>();
		component.NoVipBG.SetActive(true);
		component.SetTitle(ScriptLocalization.Get("Game_Common/vip_acc_withoutVipAcc"));
		component.ShowRewards(_rewardListSimple);
		component.ActiveFrame.SetActive(!_isActiveVip);
		_vipReward = UnityEngine.Object.Instantiate(panelEndGameExample);
		_vipReward.transform.SetParent(grid.transform);
		_vipReward.transform.localScale = Vector3.one;
		_vipReward.SetActive(true);
		PanelEndGameReward component2 = _vipReward.GetComponent<PanelEndGameReward>();
		component2.VipBG.SetActive(true);
		component2.SetTitle(ScriptLocalization.Get("Game_Common/vip_acc_withVipAcc"));
		component2.ShowRewards(_rewardListVip);
		component2.ActiveFrame.SetActive(_isActiveVip);
		component2.ButtonBuyVip.SetActive(!_isActiveVip);
		component2.LabelBuyVip.SetActive(_isActiveVip);
		grid.repositionNow = true;
	}

	public void CalculateReward()
	{
		if (_isCalculateReward)
		{
			return;
		}
		_leaderTime = PlayerBehavior.MyPlayer.leaderTime;
		if (PlayerBehavior.MyPlayer != null && PlayerBehavior.MyPlayer.leaderTime > 0f)
		{
			CupsManager.LeaderTime = PlayerBehavior.MyPlayer.leaderTime;
			CupsManager.LeaderTime = 0f;
		}
		_rewardForSave = SessionReputationManager.Instance.GetRewardList();
		_rewardListSimple = new List<SessionReputationReward>(_rewardForSave);
		if (CupsManager.Cups > GameController.instance.StartCups)
		{
			SessionReputationReward sessionReputationReward = new SessionReputationReward();
			sessionReputationReward.type = SessionReputationRewardType.Star;
			sessionReputationReward.rewardCount = CupsManager.Cups - GameController.instance.StartCups;
			CupsManager.Cups = GameController.instance.StartCups;
			CupsManager.CurrentLeagueIndex = GameController.instance.StartLeagueIndex;
			_rewardListSimple.Add(sessionReputationReward);
			_sessionCups = sessionReputationReward.rewardCount;
		}
		foreach (SessionReputationReward item in _rewardListSimple)
		{
			_rewardListVip.Add(AccountVipBonusChanger.GetInstance().ModifyReward(item));
		}
		_sessionReputationChanged = SessionReputationManager.Instance.SessionReputationChanged;
		_sessionDeadCount = CupsManager.SesionDeadCount;
		_sessionKillCount = CupsManager.SesionKillCount;
		_isCalculateReward = true;
	}

	public void GiveRewards()
	{
		if (!_blockGiveAward)
		{
			_blockGiveAward = true;
			List<SessionReputationReward> listReward = ((!_isActiveVip) ? _rewardListSimple : _rewardListVip);
			SessionReputationManager.Instance.GiveReward(button.transform.position, listReward, _callback == null);
			if (_hasSaveReward)
			{
				ResetSaveReward();
				_hasSaveReward = false;
			}
			Close();
			isGiveReward = true;
			if (PanelEndGameRewardUI.giveReward != null)
			{
				PanelEndGameRewardUI.giveReward();
			}
		}
	}

	private void Close()
	{
		_isShow = false;
		if (_callback != null)
		{
			_callback();
		}
		else
		{
			base.transform.parent.gameObject.SetActive(false);
		}
	}

	public void BuyAccountVip()
	{
		panelAccountVipBuy.SetActive(true);
	}

	private IEnumerator SetCountLabel(UILabel label, int count, int startCount = 0)
	{
		while (animationInProgress)
		{
			yield return null;
		}
		if (count < 2)
		{
			label.text = count.ToString();
			yield break;
		}
		counter = startCount;
		animationInProgress = true;
		UILabel label2 = default(UILabel);
		HOTween.To(this, 1f, new TweenParms().Prop("counter", count).OnUpdate((TweenDelegate.TweenCallback)delegate
		{
			label2.text = counter.ToString();
		}).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			BCWAudioResource.Instance.Stop("button_02");
			animationInProgress = false;
		})
			.UpdateType(UpdateType.TimeScaleIndependentUpdate));
	}

	public bool ApplySaveReward()
	{
		List<SessionReputationReward> list = ParseSavedReward("keySaveRewardSimple");
		_sessionCups = BCWStorage.Get<int>("keySessionCups");
		if (_sessionCups > 0)
		{
			SessionReputationReward sessionReputationReward = new SessionReputationReward();
			sessionReputationReward.type = SessionReputationRewardType.Star;
			sessionReputationReward.rewardCount = _sessionCups;
			list.Add(sessionReputationReward);
		}
		if (list.Count > 0)
		{
			_rewardListSimple = list;
			foreach (SessionReputationReward item in _rewardListSimple)
			{
				_rewardListVip.Add(AccountVipBonusChanger.GetInstance().ModifyReward(item));
			}
			_sessionReputationChanged = PlayerPrefs.GetInt("keySessionReputationChanged");
			_sessionDeadCount = PlayerPrefs.GetInt("keySesionDeadCount");
			_sessionKillCount = PlayerPrefs.GetInt("keySesionKillCount");
			_leaderTime = PlayerPrefs.GetFloat("keySessionleaderTime");
			_isCalculateReward = true;
			_hasSaveReward = true;
			return true;
		}
		return false;
	}

	public void SaveRewardOnQuit()
	{
		if (!_blockGiveAward)
		{
			SaveReward(_rewardForSave, "keySaveRewardSimple");
			BCWStorage.Set("keySessionCups", _sessionCups, true);
			PlayerPrefs.SetInt("keySessionReputationChanged", _sessionReputationChanged);
			PlayerPrefs.SetInt("keySesionDeadCount", _sessionDeadCount);
			PlayerPrefs.SetInt("keySesionKillCount", _sessionKillCount);
			PlayerPrefs.SetFloat("keySessionleaderTime", _leaderTime);
			PlayerPrefs.SetFloat("keySessionCups", _sessionCups);
		}
	}

	private void ResetSaveReward()
	{
		BCWStorage.Set("keySaveRewardSimple", string.Empty, true);
		BCWStorage.Set("keySessionCups", 0, true);
		PlayerPrefs.SetInt("keySessionReputationChanged", 0);
		PlayerPrefs.SetInt("keySesionDeadCount", 0);
		PlayerPrefs.SetInt("keySesionKillCount", 0);
		PlayerPrefs.SetFloat("keySessionleaderTime", 0f);
		PlayerPrefs.Save();
	}

	private void SaveReward(List<SessionReputationReward> listReward, string keySaveReward)
	{
		if (listReward.Count != 0)
		{
			string text = string.Empty;
			for (int i = 0; i < listReward.Count; i++)
			{
				text = text + listReward[i].index + " ";
			}
			BCWStorage.Set(keySaveReward, text, true);
		}
	}

	private List<SessionReputationReward> ParseSavedReward(string keySaveReward)
	{
		string text = BCWStorage.Get<string>(keySaveReward);
		string text2 = string.Empty;
		List<SessionReputationReward> list = new List<SessionReputationReward>();
		string text3 = text;
		for (int i = 0; i < text3.Length; i++)
		{
			char c = text3[i];
			if (c != ' ')
			{
				text2 += c;
				continue;
			}
			BCWDebug.LogError(text2);
			int result = 0;
			int.TryParse(text2, out result);
			list.Add((SessionReputationReward)SessionReputationManager.Instance.RewardList[result]);
			text2 = string.Empty;
		}
		return list;
	}
}
